package com.megvii.corelib.util;

import com.megvii.corelib.datastructure.LimitedQueue;

/**
 * Created by linyue on 16/2/2.
 * 用来计数的工具，判断在一定时间内的计数是否到了一个值
 */
public class TimeLimitCounter
{
    private LimitedQueue<Long> times;

    private long time;

    private int count;

    public TimeLimitCounter(long time, int count)
    {
        this.time = time;
        this.count = count;
        this.times = new LimitedQueue<>(this.count);
    }

    public boolean countAndTest()
    {
        long currentTime = System.currentTimeMillis();
        times.add(currentTime);
        if (times.size() >= count)
        {
            long firstClickSettingsTime = times.peekTail();
            if (currentTime - firstClickSettingsTime < time)
            {
                return true;
            }
        }
        return false;
    }

    public void clearCount()
    {
        times.clear();
    }
}
